﻿/// -----------------------------------------------------------------------
/// (c) 2009 Microsoft Corporation -- All rights reserved
/// This code is licensed under the MS-PL
/// http://www.opensource.org/licenses/ms-pl.html
/// Courtesy of the Open Source Techology Center: http://port25.technet.com
/// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.XPath;
using System.Xml;
using System.Text.RegularExpressions;

namespace mkProject {
    public class LinkerCommandLine : TargetCommandLine {

        public LinkerCommandLine(string commandLine)
            : base(commandLine) {
            cmd = commandLine;

            Regex regEx = new Regex(@"\b(\S*?\.res)\b", RegexOptions.IgnoreCase);
            MatchCollection matches = regEx.Matches(cmd);
            foreach (Match file in matches) {
                resourceFiles.Add(MakeProject.App.Redirect(UnMuckString(file.Captures[0].Value.Trim())));
            }

            // input lib files
            regEx = new Regex(@"\s([^/-]\S*?\.lib)\b", RegexOptions.IgnoreCase);
            matches = regEx.Matches(cmd);
            foreach (Match file in matches) {
                libFiles.Add(MakeProject.App.Redirect(UnMuckString(file.Captures[0].Value.Trim())));
            }

            regEx = new Regex(@"[/-]libpath:(\S*)", RegexOptions.IgnoreCase);
            foreach (string cmdSwitch in switches) {
                matches = regEx.Matches(cmdSwitch);
                foreach (Match path in matches) {
                    libPaths.Add(UnMuckString(path.Groups[1].Value.Trim()));
                }
            }


            regEx = new Regex("[/-]out:(.*)\\\\(\\S*)\\.(\\S*)", RegexOptions.IgnoreCase);
            foreach (string cmdSwitch in switches) {
                matches = regEx.Matches(cmdSwitch);
                foreach (Match output in matches) {
                    OutputFilename = output.Groups[2].Value;
                    OutputExtension = output.Groups[3].Value;
                    OutputRelativeDirectory = output.Groups[1].Value;

                    UniqueName = string.Format("{0}_{1}", Regex.Replace(OutputRelativeDirectory, "[^a-zA-Z]", "_"), OutputFilename);
                }
            }

            if (string.IsNullOrEmpty(UniqueName)) {
                regEx = new Regex("[/-]Fe:?(.*)\\\\(\\S*)\\.(\\S*)", RegexOptions.IgnoreCase);
                foreach (string cmdSwitch in switches) {
                    matches = regEx.Matches(cmdSwitch);
                    foreach (Match output in matches) {
                        OutputFilename = output.Groups[2].Value;
                        OutputExtension = output.Groups[3].Value;
                        OutputRelativeDirectory = output.Groups[1].Value;
                        UniqueName = string.Format("{0}_{1}", Regex.Replace(OutputRelativeDirectory, "[^a-zA-Z]", "_"), OutputFilename);
                    }
                }
            }

            // 

            if (string.IsNullOrEmpty(UniqueName))
                throw new Exception("Unable to determine linker output");
        }
    }
}
